<?

/***************************************************************************
Jupiter Content System @ Jupiterportal.org
 
Copyright (C) 2005-2007 Cosmin Flavius (highstrike@gmail.com)
Copyright (C) 2008-2009 George Mark (gmark@jupiterportal.org)

This program is free software; you can redistribute it and/or modify it under the terms of the GNU 
General Public License as published by the Free Software Foundation; either version 3 of the License, 
or (at your option) any later version.

More Info About The License At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
// Set Variables
//-----------------------------------------------------------------

if(!isset($layout)) $layout = NULL;
if(!isset($idx)) $idx = 1;

//-----------------------------------------------------------------
//  Begin Switch
//-----------------------------------------------------------------

switch($a)
{
	//-----------------------------------------------------------------
	//  Add Partner (form)
	//-----------------------------------------------------------------
	
	case 1:
	
		//-----------------------------------------------------------------
		//  Filter Access Rights
		//-----------------------------------------------------------------

		if(isset($is_guest) || isset($is_user) || isset($is_moderator)) { header("location: $PHP_SELF?i=2"); exit; }
		
		//-----------------------------------------------------------------
		//  Create Layout
		//-----------------------------------------------------------------		
		?>
		<table class="main" cellspacing="1" cellpadding="4">
		<tr class='con1'><td><a href='<?= $PHP_SELF ?>?n=modules/admin'><?= $language['Admin name'] ?></a> &raquo; <a href="<?= $PHP_SELF ?>?n=modules/partners"><?= $language['Partners Title'] ?></a> &raquo; <?= $language['Partners desc2'] ?></td></tr>
		</table>
		<table><tr><td></td></tr></table>
		
		<form method="post" action="<?= $PHP_SELF ?>?n=modules/partners&a=2">
		<table class='main' cellpadding='4' cellspacing='1'>
		<col width='30%'><col width='70%'>
		<tr class='head'><td colspan='2'><?= $language['Partners desc2'] ?></td></tr>
		<tr class='bottom'><td colspan='2'><?= $language['Partners desc3'] ?></td></tr>
		<tr class='con1'><td><span style='color:green;'><?= $language['Partners desc4'] ?></span><td><input type='text' name='name' class='box' style='width: 100%;' /></td></tr>
		<tr class='con1'><td><span style='color:green;'><?= $language['Partners desc5'] ?></span><td><input type='text' name='url' class='box' style='width: 100%;' /></td></tr>
		<tr class='con1'><td colspan='2' align='right'><input type='submit' name='submit' value='<?= $language['Partners desc2'] ?>' class='box' /></td></tr>
		</table>
		</form>
		
		<?
	break;
	
	//-----------------------------------------------------------------
	//  Add Partner (process)
	//-----------------------------------------------------------------
	
	case 2:
		
		//-----------------------------------------------------------------
		//  Do Checks
		//-----------------------------------------------------------------
		
		if(!$_POST['name']) { header("location: $PHP_SELF?n=modules/partners&a=1&i=142"); exit; }
		if(!$_POST['url']) { header("location: $PHP_SELF?n=modules/partners&a=1&i=143"); exit; }
		if(!eregi('^(http|ftp)://(.*)', $_POST['url'])) { header("location: $PHP_SELF?n=modules/partners&a=1&i=144"); exit; }
		//		Commented this line and replaced it with the one from above to allow more flexibility in adding partners URL's
		//		Date changed: 6:34 AM 12/26/2008
		//		if(!eregi('^http://www.[A-Za-z0-9]+.[A-Za-z]{2,4}$', $_POST['url'])) { header("location: $PHP_SELF?n=modules/partners&a=1&i=144"); exit; }
		
		//-----------------------------------------------------------------
		//  Do Checks
		//-----------------------------------------------------------------

		$name = jcm_htmlspecialchars(trim(jcm_stripslashes(do_censor($_POST['name']))));
		$url = jcm_htmlspecialchars(trim(jcm_stripslashes(do_censor($_POST['url']))));
		
		//-----------------------------------------------------------------
		//  Add To Database
		//-----------------------------------------------------------------
		
		$info['name'] = $name;
		$info['url'] = $url;
		$db->insertRow("partners",$info);
		
		//-----------------------------------------------------------------
		//  Add Log
		//-----------------------------------------------------------------
		
		$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc3']).' <b>partners</b> the partner -> \'<a href="'.$db->escape($url).'">'.$db->escape($name).'</a>\'','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Redirect
		//-----------------------------------------------------------------
		
		header("location: $PHP_SELF?n=modules/partners&i=145");
		
	break;
	
	//-----------------------------------------------------------------
	//  Edit Partner (form)
	//-----------------------------------------------------------------
	
	case 3:
	
		//-----------------------------------------------------------------
		//  Filter Access Rights
		//-----------------------------------------------------------------

		if(isset($is_guest) || isset($is_user) || isset($is_moderator)) { header("location: $PHP_SELF?i=2"); exit; }
		
		//-----------------------------------------------------------------
		//  Get Partner
		//-----------------------------------------------------------------

		$partner = $db->getLine("SELECT id, name, url FROM partners",$d);
		
		//-----------------------------------------------------------------
		//  Create Layout
		//-----------------------------------------------------------------		
		?>
		
		<table class='main' cellpadding='4' cellspacing='1'><tr class='con1'><td><a href="<?= $PHP_SELF ?>?n=modules/partners"><?= $language['Partners Title'] ?></a> &raquo; <?= $language['Partners desc6'] ?></td></tr></table>
		
		<table><tr><td></td></tr></table>
		
		<form method="post" action="<?= $PHP_SELF ?>?n=modules/partners&a=4&d=<?= $d ?>">
		<table class='main' cellpadding='4' cellspacing='1'>
		<col width='30%'><col width='70%'>
		<tr class='head'><td colspan='2'><?= $language['Partners desc6'] ?> [<?= $partner['name'] ?>]</td></tr>
		<tr class='bottom'><td colspan='2'><?= $language['Partners desc3'] ?></td></tr>
		<tr class='con1'><td><span style='color:green;'><?= $language['Partners desc4'] ?></span><td><input type='text' name='name' value='<?= $partner['name'] ?>' class='box' style='width: 100%;' /></td></tr>
		<tr class='con1'><td><span style='color:green;'><?= $language['Partners desc5'] ?></span><td><input type='text' name='url' value='<?= $partner['url'] ?>' class='box' style='width: 100%;' /></td></tr>
		<tr class='con1'><td colspan='2' align='right'><input type='submit' name='submit' value='<?= $language['Partners desc6'] ?> [<?= $partner['name'] ?>]' class='box' /></td></tr>
		</table>
		</form>
		
		<?
	break;
	
	//-----------------------------------------------------------------
	//  Edit Partner (proccess)
	//-----------------------------------------------------------------
	
	case 4:
	
		//-----------------------------------------------------------------
		//  Do Checks
		//-----------------------------------------------------------------
		
		if(!$_POST['name']) { header("location: $PHP_SELF?n=modules/partners&a=3&d=$d&i=142"); exit; }
		if(!$_POST['url']) { header("location: $PHP_SELF?n=modules/partners&a=3&d=$d&i=143"); exit; }
		if(!eregi('^http://[A-Za-z0-9]+.[A-Za-z]{2,4}/$', $_POST['url'])) { header("location: $PHP_SELF?n=modules/partners&a=3&d=$d&i=144"); exit; }
		
		//-----------------------------------------------------------------
		//  Do Checks
		//-----------------------------------------------------------------

		$name = jcm_htmlspecialchars(trim(jcm_stripslashes(do_censor($_POST['name']))));
		$url = jcm_htmlspecialchars(trim(jcm_stripslashes(do_censor($_POST['url']))));
		
		//-----------------------------------------------------------------
		//  Add To Database
		//-----------------------------------------------------------------
		
		$info['name'] = $name;
		$info['url'] = $url;
		$db->updateRow("partners",$info,"id = $d");
		
		//-----------------------------------------------------------------
		//  Add Log
		//-----------------------------------------------------------------
		
		$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => ''.$db->escape($language['Logs desc2']).' <b>partners</b> the partner -> \'<a href="'.$db->escape($url).'">'.$db->escape($name).'</a>\'','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Redirect
		//-----------------------------------------------------------------
		
		header("location: $PHP_SELF?n=modules/partners&i=146");
	break;
	
	//-----------------------------------------------------------------
	//  Delete Partner
	//-----------------------------------------------------------------
	
	case 5:
	
		//-----------------------------------------------------------------
		//  Filter Access Rights
		//-----------------------------------------------------------------

		if(isset($is_guest) || isset($is_user) || isset($is_moderator)) { header("location: $PHP_SELF?i=2"); exit; }
		
		//-----------------------------------------------------------------
		//  Get Partner
		//-----------------------------------------------------------------

		$partner = $db->getLine("SELECT id, name, url FROM partners",$d);
		
		//-----------------------------------------------------------------
		//  Write to log
		//-----------------------------------------------------------------

		$db->insertRow("log",array('user_id' => ''.$_SESSION["$security_session"]['id'].'','user_user' => ''.$_SESSION["$security_session"]['username'].'','log' => ''.$language['Logs desc'].' <b>partners</b> the partner -> \''.$partner['name'].'\'','date' => ''.time().''));
		
		//-----------------------------------------------------------------
		//  Delete from the database
		//-----------------------------------------------------------------

		$db->deleteRow("partners","id = '$d'");
		
		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/partners&i=147");
	
	break;
	
	//-----------------------------------------------------------------
	//  Show Partners
	//-----------------------------------------------------------------
	
	default:
	
		//-----------------------------------------------------------------
		//  Navigation Stuff
		//-----------------------------------------------------------------

		$rows_per_page = $config['nav_partners'];
		$numrows = $db->getInfo_countRows("partners");

		$lastpage = ceil($numrows/$rows_per_page);
		$p = (int)$p;

		if ($p < 1) $p = 1;
		elseif ($p > $lastpage) $p = $lastpage;

		if($numrows != FALSE) $limit = 'LIMIT ' .($p - 1) * $rows_per_page .',' .$rows_per_page;
		else $limit = NULL;
	
		//-----------------------------------------------------------------
		//  Get Partners From DB
		//-----------------------------------------------------------------	
		
		$partners = $db->getTable("SELECT id, name, url FROM partners ORDER BY 'name' ASC $limit");

		if($partners != FALSE)
		{
		
			foreach($partners as $idx=> $partner)
			{
				$idx++;
				$layout .= "<tr class='".row2color($idx)."'>";
				$layout .= "<td><a href='{$partner['url']}' title='{$partner['name']}'>{$partner['name']}</a></td>";
				$layout .= "<td><span class='inv'>{$partner['url']}</span></td>";
				
				if(isset($is_webmaster))
				{
					$layout .= "<td align='center' width='20px'>";
					$layout .= "<a href='$PHP_SELF?n=modules/partners&a=3&d={$partner['id']}'><img src='templates/{$template}/images/buttons/edit.gif' alt='Edit Partner' border='0' /></a>";
					$layout .= "<a href='$PHP_SELF?n=modules/partners&a=5&d={$partner['id']}'><img src='templates/{$template}/images/buttons/delete.gif' alt='Edit Partner' border='0' /></a>";
					$layout .= "</td>";
				}
				
				$layout .= "</tr>";
			}
		}else $layout .= "<tr class='con1'><td colspan='2'>{$language['Partners desc1']}</td></tr>";
		
		//-----------------------------------------------------------------
		//  Navigation Code
		//-----------------------------------------------------------------

		if ($p == 1) $nav_prev = "<tr><td class='con1' align='right'>&nbsp;</td>";
		else
		{
			$prevpage = $p-1;
			$nav_prev = "<tr><td class='con1' align='left'><a href='$PHP_SELF?n=modules/partners&p=$prevpage'>&laquo; {$language['Partners previous']}</a></td>";
		}

		$nav_nr = '[ '.paginate($lastpage, $p, $PHP_SELF.'?n=modules/partners').' ]';

		if($idx == $numrows) $nav_nr = NULL;

		if ($p == $lastpage)
			$nav_next = "<td class='con1' align='right'>&nbsp;</td></tr>";
		else
		{
			$nextcode = $numrows - $idx * $p;
			if($nextcode > $idx) $how_next = $idx;
			else $how_next = $nextcode;

			$nextpage = $p+1;

			$nav_next = "<td class='con1' align='right'><a href='$PHP_SELF?n=modules/partners&p=$nextpage'>{$language['Partners next']} $how_next &raquo;</a></td></tr>";
		}

		if($idx == $numrows)
		{
			$nav_head = "class='con1'";
			$nav = NULL;
		}
		else
		{
			$nav_head = "class='head'";
			$nav = ''.$nav_prev.''.$nav_next.'';
		}
		
		//-----------------------------------------------------------------
		//  Create Template
		//-----------------------------------------------------------------	
	?>
		<table class='main' cellpadding='4' cellspacing='1'><tr class='con1'><td><?= $language['Partners Title'] ?></td></tr></table>
		
		<table><tr><td></td></tr></table>
		
		<table class='main' cellpadding='4' cellspacing='1'>
		<col width='45%'><col width='45%'>
		<tr class='head'><td colspan='3'><?= $language['Partners Title'] ?></td></tr>
		<?= $layout ?>
		</table>
		
		<? /* DO NOT TOUCH THIS LINE */ if($partners != FALSE): ?>
		<table><tr><td></td></tr></table>
		<table class="main" cellspacing="1" cellpadding="4" width="100%">
		<col width="50%"><col width="50%">
		<tr <?= $nav_head ?>>
		<td colspan="2">
		<table border="0" cellspacing="0" cellpadding="0" width="100%">
		<col width="50%"><col width="50%">
		<tr class="empty">
		<td align="left"><?= $language['Partners nav'] ?> <b><?= $idx ?></b> <?= $language['Partners nav2'] ?> <b><?= $numrows ?></b></td>
		<td align="right"><?= $nav_nr ?></td>
		</tr>
		</table>
		</td>
		</tr>
		<?= $nav ?>
		</table>
		<? /* DO NOT TOUCH THIS LINE */ endif; ?>
	<?
	break;
}
?>
